/********************************************************************************
* Description: 
* Author: hsqiang
* Create Date: 2023-06-22
********************************************************************************/
#ifndef ACCEPTOR_HPP_
#define ACCEPTOR_HPP_

#include "channel.hpp"
#include "end_point.hpp"
#include "noncopyable.hpp"
#include "net_event_callback.hpp"
#include <atomic>
#include <map>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <error.h>
#include <fcntl.h>

namespace event_framework
{
    class EventLoop;
    class Acceptor final : Noncopyable
    {
    public:
        Acceptor(EventLoop& loop_, Socket& s) : loop(loop_), socket(s) {}
        ~Acceptor() = default;
        void SetNewConnCallback(NewConnCallback&& func)
        {
            ASSERT(func);
            callback = std::move(func);
        }
        bool Listen(int backlog);
        void Fini();
        bool Avaliable() const { return !finished; }
    private:
        void Accept();
        
        std::atomic_bool finished { false };
        int idleFd { -1 };
        EventLoop& loop;
        Socket& socket;
        ChannelPtr acceptChannel;
        NewConnCallback callback;
    };
    using AcceptorPtr = std::unique_ptr<Acceptor>;
}

#endif